package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKwlqdBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKwlqdBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntKwlqdBsInfoBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntKwlqdBsInfoVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKwlqdBsInfo;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKwlqdBsInfoMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntKwlqdBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥混凝土抗弯拉强度试验检测记录Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntKwlqdBsInfoServiceImpl implements IReportSnhntKwlqdBsInfoService {

    private final ReportSnhntKwlqdBsInfoMapper baseMapper;
    private final ReportSnhntKwlqdBaDetailMapper detailMapper;

    /**
     * 查询水泥混凝土抗弯拉强度试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportSnhntKwlqdBsInfoVo queryById(Long excelId) {
        ReportSnhntKwlqdBsInfoVo vo = baseMapper.selectVoById(excelId);
        List<ReportSnhntKwlqdBaDetail> detailList = detailMapper.selectByExcelId(excelId);
        if (CollUtil.isNotEmpty(detailList)) {
            vo.setDetailList(detailList);
        }
        return vo;
    }

    /**
     * 查询水泥混凝土抗弯拉强度试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportSnhntKwlqdBsInfoVo> queryPageList(ReportSnhntKwlqdBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntKwlqdBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntKwlqdBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥混凝土抗弯拉强度试验检测记录列表
     */
    @Override
    public List<ReportSnhntKwlqdBsInfoVo> queryList(ReportSnhntKwlqdBsInfoBo bo) {
        LambdaQueryWrapper<ReportSnhntKwlqdBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntKwlqdBsInfo> buildQueryWrapper(ReportSnhntKwlqdBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntKwlqdBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSnhntKwlqdBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSnhntKwlqdBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSnhntKwlqdBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSnhntKwlqdBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSnhntKwlqdBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSnhntKwlqdBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSnhntKwlqdBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSnhntKwlqdBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSnhntKwlqdBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportSnhntKwlqdBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportSnhntKwlqdBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getLaiysj() != null, ReportSnhntKwlqdBsInfo::getLaiysj, bo.getLaiysj());
        lqw.eq(bo.getShiyjcrq() != null, ReportSnhntKwlqdBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytj()), ReportSnhntKwlqdBsInfo::getShiytj, bo.getShiytj());
        lqw.eq(StringUtils.isNotBlank(bo.getWend()), ReportSnhntKwlqdBsInfo::getWend, bo.getWend());
        lqw.eq(StringUtils.isNotBlank(bo.getXiangdsd()), ReportSnhntKwlqdBsInfo::getXiangdsd, bo.getXiangdsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSnhntKwlqdBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSnhntKwlqdBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSnhntKwlqdBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSnhntKwlqdBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getShejqddj()), ReportSnhntKwlqdBsInfo::getShejqddj, bo.getShejqddj());
        lqw.eq(StringUtils.isNotBlank(bo.getBanhfs()), ReportSnhntKwlqdBsInfo::getBanhfs, bo.getBanhfs());
        lqw.eq(bo.getZhizjj() != null, ReportSnhntKwlqdBsInfo::getZhizjj, bo.getZhizjj());
        lqw.eq(StringUtils.isNotBlank(bo.getChengxff()), ReportSnhntKwlqdBsInfo::getChengxff, bo.getChengxff());
        lqw.eq(StringUtils.isNotBlank(bo.getYanghff()), ReportSnhntKwlqdBsInfo::getYanghff, bo.getYanghff());
        lqw.eq(StringUtils.isNotBlank(bo.getTanld()), ReportSnhntKwlqdBsInfo::getTanld, bo.getTanld());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSnhntKwlqdBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSnhntKwlqdBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSnhntKwlqdBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSnhntKwlqdBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSnhntKwlqdBsInfo::getRiq, bo.getRiq());
        lqw.eq(bo.getHanslpjz() != null, ReportSnhntKwlqdBsInfo::getHanslpjz, bo.getHanslpjz());
        return lqw;
    }

    /**
     * 新增水泥混凝土抗弯拉强度试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportSnhntKwlqdBsInfoBo bo) {
        ReportSnhntKwlqdBsInfo add = BeanUtil.toBean(bo, ReportSnhntKwlqdBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            List<ReportSnhntKwlqdBaDetail> detailList = new ArrayList<>();
            for (int i = 0; i < 18; i++) {
                ReportSnhntKwlqdBaDetail detail = new ReportSnhntKwlqdBaDetail();
                detail.setExcelId(bo.getExcelId());
                detailList.add(detail);
            }
            detailMapper.insertBatch(detailList);
        }
        return flag;
    }

    /**
     * 修改水泥混凝土抗弯拉强度试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportSnhntKwlqdBsInfoBo bo) {
        ReportSnhntKwlqdBsInfo update = BeanUtil.toBean(bo, ReportSnhntKwlqdBsInfo.class);
        validEntityBeforeSave(update);
        //更新子表
        List<ReportSnhntKwlqdBaDetail> detailList = bo.getDetailList();
        if (detailList != null && detailList.size() != 0) {
            detailMapper.updateBatchById(detailList);
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntKwlqdBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥混凝土抗弯拉强度试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
